// pages/CurrentOrderList/CurrentOrderList.js
var api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tuangouId:'',
    targetData:{},
    slideButtons:[
      {
        text: '取消',
        type:'warn',
        extClass: 'test',
        nowIndex:9999
    },
    ]
  },
  onSlideShow(e) {
    var id = e.currentTarget.dataset.id
    this.setData({
      nowIndex: id,
    })
  },
  hideSelectUserModal(){
    this.setData({showSelectUser: false,});
  },
  slideButtonTap:function(e){
    var that =this;
    wx.showModal({
      title: '',
      content: '取消此订单',
      showCancel:true,
      cancelText:'我再想想',
      confirmText:'立即取消',
      complete: (res) => {

        if (res.confirm) {
          var id = e.currentTarget.dataset.id
          console.log('vccc='+id)
          var pro = that.data.targetData.specsList[id];
          api.request1({"specsId":pro.specsId, "path": "user/deleteUserSpecs","token":wx.getStorageSync('token')}, function (result) {
            console.log('result.code='+result.code);
            if(result.code=='0'){
              that.getTargetDetail(that.data.tuangouId)
            }  
          }, function (error) {
            console.log('error==')
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    that.data.tuangouId = options.tuangouId;
    console.log('dddd'+that.data.tuangouId)
    that.setData({
      isIphoneX: getApp().globalData.statusBarHeight>=44?true:false,
      btmHeight:120,
      
    })
    that.getTargetDetail(options.tuangouId)
  },
  getTargetDetail:function(tuangouID){
    var that =this;
    api.request1({"tuangouId":tuangouID, "path": "user/tuangouDetail","token":wx.getStorageSync('token')}, function (result) {
      wx.stopPullDownRefresh()
      if(result.code=='0'){
        that.data.targetData = result.data;
        that.setData({
          targetData:result.data,
          nowIndex:9999
        })
        
      }  
    }, function (error) {
      console.log('error==')
    })
  },
  buyMorePro:function(){
    var that = this
    wx.reLaunch({
      url: '/pages/HomePage/HomePage?id='+that.data.tuangouId+'&buyProAgain='+'true'+'&userId='+ wx.getStorageSync('leaderId'),
    })
  },
  reachTakeOrder:function(){
    var that = this

    var specsId = ''
    for(var i=0;i<that.data.targetData.specsList.length;i++){
      var pro = that.data.targetData.specsList[i];
      if(i==that.data.targetData.specsList.length-1){
        specsId=specsId+pro.specsId
      }else{
        specsId=specsId+pro.specsId+','
      }
      
    }

    console.log('ddd===='+specsId)
    api.request1({ "path": "user/come","specsId":specsId,"token":wx.getStorageSync('token')}, function (result) {     
      if(result.code=='0'){
        that.setData({
          showSelectUser:true,
          number:result.data.number
        })
        that.getTargetDetail(that.data.tuangouId)
      }else if(result.code == '301'){
        
        wx.showModal({
          title: '',
          content: result.msg,
          showCancel:false,
          confirmText:'我知道了',
          confirmColor:'#60C371',
          complete: (res) => {

            if (res.confirm) {
              that.getTargetDetail(that.data.tuangouId)
            }
          }
        })
      }
    }, function (error) {
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    var that = this
    that.getTargetDetail(that.data.tuangouId);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})